package com.mm.controller.system;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mm.entity.MmDictionary;
import com.mm.exception.BusinessException;
import com.mm.service.impl.MmDictionaryServiceImpl;
import com.mm.enums.ResultCodeEnum;
import com.mm.utils.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
* <p>
* 字典表 前端控制器
* </p>
*
* @author wdx
* @since 2020-09-17
*/
@Api(tags = "字典API")
@Validated
@RestController
@RequestMapping("/mm-dictionary")
public class MmDictionaryController {

    @Autowired
    MmDictionaryServiceImpl mmDictionaryService;

    @ApiOperation(value = "分页列表")
    @GetMapping(value = "/page")
    @ApiImplicitParams(value = {
        @ApiImplicitParam(name = "pageNo", value = "当前页", defaultValue = "1", dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "一页条数", defaultValue = "10", dataType = "int", paramType = "query")
    })
    public ApiResult<MmDictionary> page(@NotNull(message = "当前页不能为空") Integer pageNo, @NotNull(message = "页条数不能为空") Integer pageSize) {
        IPage<MmDictionary> page = mmDictionaryService.page(new Page<>(pageNo, pageSize));
        return ApiResult.success(page);
    }

    @ApiOperation(value = "保存")
    @GetMapping(value = "/save")
    public ApiResult save(@RequestBody MmDictionary mDictionary) {
        boolean save = mmDictionaryService.save(mDictionary);
        if (!save){
            throw new BusinessException();
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "详情")
    @GetMapping(value = "/detail")
    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query")
    public ApiResult<MmDictionary> detail(@NotBlank(message = "ID不能为空") Integer id) {
        MmDictionary mDictionary = mmDictionaryService.getById(id);
        if (mDictionary == null) {
            throw new BusinessException(ResultCodeEnum.NO_DATA);
        }
       return ApiResult.success();
    }


    @ApiOperation(value = "修改")
    @GetMapping(value = "/update")
    public ApiResult update(@RequestBody MmDictionary mDictionary) {
        boolean update = mmDictionaryService.updateById(mDictionary);
        if (!update) {
            throw new BusinessException();
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query")
    @GetMapping(value = "/del")
    public ApiResult del(@NotBlank(message = "ID不能为空") Integer id) {
        boolean del = mmDictionaryService.removeById(id);
        if (!del) {
            throw new BusinessException();
        }
        return ApiResult.success();
    }

}
